;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Merchant.ApplyAudit", [])
        .controller("ApplyAuditCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                curRow: 0
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.detailInfo = {
                isSap:0,
                isBarcode:0
            };

            $scope.tableConfig = {
                fixedWidth: 80,
                scrollWidth: 870,
                tableCol: [
                    {
                        name: "组织机构代码",
                        dataField: "orgCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "企业名称",
                        dataField: "companyName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "邀请方",
                        dataField: "senderName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "邀请日期",
                        dataField: "createdDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "审核人",
                        dataField: "auditName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "审核状态",
                        dataField: "statusView",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        name: "审核意见",
                        dataField: "auditRecord",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        dataField: "",
                        name: "操作",
                        width: 80,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.statusList = [
                {
                    id: 1,
                    name: "待审核"
                },
                {
                    id: 2,
                    name: "审核驳回"
                },
                {
                    id: 3,
                    name: "审核通过"
                }
            ];

            var btnPermissionCode = {
                companyAudit: window.ActionCode.companyAudit
            };

            $scope.btnPermission = {
                companyAudit: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {};
                $scope.detailFlag = [true, true];
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    curRow: 0
                };
                var service = "device";
                var reqUrl = "/company/applyRecordList";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createdDateShow = $filter("YbDate")(item.createdDate);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show"+ toolbar]) {
                    $scope.toolbarConfig["show"+ toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show"+ toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.auditData = function (item) {
                $scope.detailInfo = item;
                $scope.changeTab(1);
            };

            $scope.saveData = function (isValid, status) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/company/audit";
                var param = {};
                param.applyId = $scope.detailInfo.applyId;
                param.auditStatus = status;
                param.auditRemark = $scope.detailInfo.auditRecord;
                param.isSap=$scope.detailInfo.isSap;
                param.sapCode=$scope.detailInfo.sapCode;
                param.isBarcode=$scope.detailInfo.isBarcode;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                setBtnPermission();
            })();
        }])
    ;
})(window, window.angular);